package com.finance.system.service;

import com.finance.base.exception.BaseException;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.system.bean.pojo.SysConfig;
import com.finance.system.dao.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by CBWL on 2017/4/24.
 */
@Service("configService")
public class ConfigService implements IConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;


    @Override
    public SysConfig getById(Long id) {
        return this.sysConfigMapper.getById(id);
    }

    @Override
    public List<SysConfig> listAll() {
        return this.sysConfigMapper.listAll();
    }

    @Override
    public PageResultBO<SysConfig> listByPage(Page page) {
        PageResultBO<SysConfig> resultBO = new PageResultBO<SysConfig>();
        List<SysConfig> list = this.sysConfigMapper.listByPage(page);
        resultBO.setDataList(list);
        resultBO.setPageSize(page.getPageSize());
        resultBO.setPageNo(page.getPageNo());
        resultBO.setTotalRecord(page.getTotalRecord());
        return resultBO;
    }

    @Override
    public List<String> keys() {
        return this.sysConfigMapper.keys();
    }

    @Override
    @Cacheable(value = "sysConfig", key = "'config_'+#key" )
    public String findConfig(String key) {
        return this.sysConfigMapper.findConfig(key);
    }


    @Override
    @CacheEvict(value = "sysConfig", key = "'config_'+#key",condition = "#result == true")
    public boolean addConfig(String key, String value) {
        return this.sysConfigMapper.addConfig(key,value)==1;
    }

    @Override
    @CacheEvict(value = "sysConfig", key = "'config_'+#key",condition = "#result == true")
    public boolean removeConfig(String key) {
        boolean result = this.sysConfigMapper.removeConfig(key) == 1;
        return result;
    }

    @Override
    @CachePut(value = "sysConfig", key = "'config_'+#key",condition = "#result != null")
    public SysConfig update(SysConfig sysConfig) throws BaseException{
        boolean result = this.sysConfigMapper.update(sysConfig) ==1;
        if(result){
            return sysConfig;
        }
        return null;
    }
}
